import { defineStore } from 'pinia'
import { LogApi } from "@/api/log/index.js";
import { ElMessage } from "element-plus";

// 定义日志管理的Pinia store
export const useLogStore = defineStore('log', {
    state: () => ({
        logList: [], // 日志列表数据
        logDetail: null, // 当前查看的日志详情
        archiveResult: null, // 归档操作结果
        statisticsData: null, // 统计报表数据
        exportData: null, // 导出数据

        // 分页信息
        pageInfo: {
            page: 1,
            size: 10,
            total: 0,
            totalPage: 0
        },

        // 查询条件
        queryParams: {
            userId: null,
            action: '',
            targetType: '',
            targetId: null,
            startTime: '',
            endTime: ''
        },

        // 归档参数
        archiveParams: {
            retainDays: 30,
            batchSize: 1000
        },

        // 统计参数
        statisticsParams: {
            timeRange: 'month', // day/week/month
            startTime: '',
            endTime: ''
        }


    }),

    actions: {
        // 获取日志分页列表
        async getLogList() {
            // 构建查询参数对象
            const params = {
                ...this.queryParams,
                page: this.pageInfo.page,
                size: this.pageInfo.size
            };

            try {
                const response = await LogApi.list(params);
                this.logList = response.data.records;

                // 更新分页信息
                this.pageInfo.page = response.data.current;
                this.pageInfo.size = response.data.size;
                this.pageInfo.total = response.data.total;
                this.pageInfo.totalPage = response.data.pages;

                return response;
            } catch (error) {
                ElMessage.error('获取日志列表失败');
                throw error;
            }
        },

        // 获取日志详情
        async getLogDetail(id) {
            try {
                const response = await LogApi.detail(id);
                this.logDetail = response.data;
                return response;
            } catch (error) {
                ElMessage.error('获取日志详情失败');
                throw error;
            }
        },

        // 归档日志
        async archiveLogs() {
            try {
                const response = await LogApi.archive(this.archiveParams);
                this.archiveResult = response.data;
                ElMessage.success(`已归档${response.data.archivedCount}条日志`);
                return response;
            } catch (error) {
                ElMessage.error('日志归档失败');
                throw error;
            }
        },

        // 导出日志
        async exportLogs() {
            try {
                const response = await LogApi.export({
                    startTime: this.queryParams.startTime,
                    endTime: this.queryParams.endTime,
                    exportType: 'excel'
                });
                this.exportData = response.data;
                ElMessage.success('导出任务已创建');
                return response;
            } catch (error) {
                ElMessage.error('日志导出失败');
                //加个弹窗提示要点击才能取消的那种

                throw error;
            }
        },

        // 获取统计报表
        async getStatistics() {
            try {
                const response = await LogApi.statistics(this.statisticsParams);
                this.statisticsData = response.data;
                return response;
            } catch (error) {
                ElMessage.error('获取统计报表失败');
                throw error;
            }
        },

        // 重置查询条件
        resetQueryParams() {
            this.queryParams = {
                userId: null,
                action: '',
                targetType: '',
                targetId: null,
                startTime: '',
                endTime: ''
            };
        },

        // 处理分页变化
        handlePageChange(page) {
            this.pageInfo.page = page;
            this.getLogList();
        }
    }
});